package com.anbow.courseware.controller;

import com.anbow.courseware.entity.DO.User;
import com.anbow.courseware.entity.DTO.TeachingQuestionsDTO;
import com.anbow.courseware.entity.DTO.UserDTO;
import com.anbow.courseware.entity.DTO.VipTeachingQuestionsDTO;
import com.anbow.courseware.entity.VO.TeachingQuestionsVO;
import com.anbow.courseware.entity.VO.VipTeachingQuestionsVO;
import com.anbow.courseware.service.impl.TeachingQuestionsServiceImpl;
import com.anbow.courseware.service.impl.UserServiceImpl;
import com.anbow.courseware.service.impl.VipTeachingQuestionsServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 用户登录
 *
 * @author zhaoYuFei
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private TeachingQuestionsServiceImpl teachingQuestionsService;
    @Autowired
    private VipTeachingQuestionsServiceImpl vipTeachingQuestionsService;

    @GetMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                        HttpSession session) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(username);
        userDTO.setPassword(password);
        User result = userService.login(userDTO);
        if (result!=null) {
            List<TeachingQuestionsVO> collect = null;
            List<TeachingQuestionsDTO> all = teachingQuestionsService.getAll();
            if (all == null || all.size() == 0) {
                collect = new ArrayList<>();
            } else {
                collect = all.stream().map(e -> {
                    TeachingQuestionsVO teachingQuestionsVO = new TeachingQuestionsVO();
                    BeanUtils.copyProperties(e, teachingQuestionsVO);
                    return teachingQuestionsVO;
                }).collect(Collectors.toList());
            }
            List<VipTeachingQuestionsVO> collect1 = null;
            List<VipTeachingQuestionsDTO> all1 = vipTeachingQuestionsService.getAll();
            if (all1 == null || all1.size() == 0) {
                collect1=new ArrayList<>();
            } else {
                collect1 = all1.stream().map(e -> {
                    VipTeachingQuestionsVO vipTeachingQuestionsVO = new VipTeachingQuestionsVO();
                    BeanUtils.copyProperties(e, vipTeachingQuestionsVO);
                    return vipTeachingQuestionsVO;
                }).collect(Collectors.toList());
            }

            session.setAttribute("username", userDTO.getUsername());
            session.setAttribute("create_user_id", result.getId());
            session.setAttribute("list", collect.subList(0, collect.size() > 10 ? 10 : collect.size()));
            session.setAttribute("vipList", collect1.subList(0, collect1.size() > 10 ? 10 : collect1.size()));
            session.setAttribute("totalVip", collect1.size());
            session.setAttribute("total", collect.size());
        } else {
            session.setAttribute("loginError", "用户名或密码错误！");
        }
        return "main";
    }
}
